﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Text;
using WorkOrder.Model;

namespace WorkOrder.Business
{
    /// <summary>
    /// 数据访问类Business_Table_Notice。
    /// </summary>
    public class Business_Table_Notice
    {
        private DbHelper dbhelper;

        public Business_Table_Notice()
        {
            dbhelper = new DbHelper();
        }

        #region  成员方法

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int NoticeID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from Table_Notice");
            strSql.Append(" where NoticeID=@NoticeID ");
            SqlParameter[] parameters = {
                                            new SqlParameter("@NoticeID", SqlDbType.Int, 4)
                                        };
            parameters[0].Value = NoticeID;

            return dbhelper.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(Model_Table_Notice model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into Table_Notice(");
            strSql.Append("Title,TypeID,Content,CreateDate,Author)");
            strSql.Append(" values (");
            strSql.Append("@Title,@TypeID,@Content,getdate(),@Author)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
                                            new SqlParameter("@Title", SqlDbType.NVarChar, 100),
                                            new SqlParameter("@TypeID", SqlDbType.Int, 4),
                                            new SqlParameter("@Content", SqlDbType.NText),
                                            new SqlParameter("@Author", SqlDbType.Int, 4)
                                        };
            parameters[0].Value = model.Title;
            parameters[1].Value = model.TypeID;
            parameters[2].Value = model.Content;
            parameters[3].Value = model.Author;

            object obj = dbhelper.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 1;
            }
            return Convert.ToInt32(obj);
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public int Update(Model_Table_Notice model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update Table_Notice set ");
            strSql.Append("Title=@Title,");
            strSql.Append("TypeID=@TypeID,");
            strSql.Append("Content=@Content");
            strSql.Append(" where NoticeID=@NoticeID ");
            SqlParameter[] parameters = {
                                            new SqlParameter("@NoticeID", SqlDbType.Int, 4),
                                            new SqlParameter("@Title", SqlDbType.NVarChar, 100),
                                            new SqlParameter("@TypeID", SqlDbType.Int, 4),
                                            new SqlParameter("@Content", SqlDbType.NText),
                                        };
            parameters[0].Value = model.NoticeID;
            parameters[1].Value = model.Title;
            parameters[2].Value = model.TypeID;
            parameters[3].Value = model.Content;


            return dbhelper.ExecuteSql(strSql.ToString(), parameters);
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public void Delete(int NoticeID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from Table_Notice ");
            strSql.Append(" where NoticeID=@NoticeID ");
            SqlParameter[] parameters = {
                                            new SqlParameter("@NoticeID", SqlDbType.Int, 4)
                                        };
            parameters[0].Value = NoticeID;

            dbhelper.ExecuteSql(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Model_Table_Notice GetModel(string Notice_ID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 NoticeID,Title,TypeID,Content,CreateDate,Author from Table_Notice ");
            strSql.Append(" where NoticeID=" + Notice_ID + " ");
            
            Model_Table_Notice model = new Model_Table_Notice();
            DataSet ds = dbhelper.Query(strSql.ToString());
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["NoticeID"].ToString() != "")
                {
                    model.NoticeID = ds.Tables[0].Rows[0]["NoticeID"].ToString();
                }
                model.Title = ds.Tables[0].Rows[0]["Title"].ToString();
                if (ds.Tables[0].Rows[0]["TypeID"].ToString() != "")
                {
                    model.TypeID = ds.Tables[0].Rows[0]["TypeID"].ToString();
                }
                model.Content = ds.Tables[0].Rows[0]["Content"].ToString();
                if (ds.Tables[0].Rows[0]["CreateDate"].ToString() != "")
                {
                    model.CreateDate = ds.Tables[0].Rows[0]["CreateDate"].ToString();
                }
                if (ds.Tables[0].Rows[0]["Author"].ToString() != "")
                {
                    model.Author = ds.Tables[0].Rows[0]["Author"].ToString();
                }
                return model;
            }
            return null;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select NoticeID,Title,A.TypeID,B.TypeName,CreateDate,Author,C.remark as AuthorrName ");
            strSql.Append("FROM Table_Notice A,Table_Notice_Type B,Table_User C where A.TypeID=B.TypeID and A.Author=C.user_id ");
            if (strWhere.Trim() != "")
            {
                strSql.Append("and " + strWhere + " order by A.CreateDate DESC");
            }
            else
            {
                strSql.Append("order by A.CreateDate DESC");
            }
            return dbhelper.Query(strSql.ToString());
        }
        

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" NoticeID,Title,A.TypeID,B.TypeName,CreateDate,Author,C.remark as AuthorrName ");
            strSql.Append("FROM Table_Notice A,Table_Notice_Type B,Table_User C where A.TypeID=B.TypeID and A.Author=C.user_id ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return dbhelper.Query(strSql.ToString());
        }

        public DataTable GetNoticeType()
        {
            return dbhelper.Query("noticeType", "select TypeID,TypeName from Table_Notice_Type");
        }

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            SqlParameter[] parameters = {
                    new SqlParameter("@tblName", SqlDbType.VarChar, 255),
                    new SqlParameter("@fldName", SqlDbType.VarChar, 255),
                    new SqlParameter("@PageSize", SqlDbType.Int),
                    new SqlParameter("@PageIndex", SqlDbType.Int),
                    new SqlParameter("@IsReCount", SqlDbType.Bit),
                    new SqlParameter("@OrderType", SqlDbType.Bit),
                    new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
                    };
            parameters[0].Value = "Table_Notice";
            parameters[1].Value = "ID";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;	
            return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        }*/

        #endregion  成员方法

        public Model_Table_Notice GetOneNotice(string s)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select top 1 NoticeID,A.TypeID,Title,B.TypeName,CreateDate,Content,Author,C.remark as AuthorName ");
            strSql.Append("FROM Table_Notice A,Table_Notice_Type B,Table_User C where A.TypeID=B.TypeID and A.Author=C.user_id and A.NoticeID="+s);

            Model_Table_Notice model = new Model_Table_Notice();
            DataSet ds = dbhelper.Query(strSql.ToString());
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["NoticeID"].ToString() != "")
                {
                    model.NoticeID = ds.Tables[0].Rows[0]["NoticeID"].ToString();
                }
                model.Title = ds.Tables[0].Rows[0]["Title"].ToString();
                model.AuthorName = ds.Tables[0].Rows[0]["AuthorName"].ToString();
                model.TypeName = ds.Tables[0].Rows[0]["TypeName"].ToString();
                if (ds.Tables[0].Rows[0]["TypeID"].ToString() != "")
                {
                    model.TypeID = ds.Tables[0].Rows[0]["TypeID"].ToString();
                }
                model.Content = ds.Tables[0].Rows[0]["Content"].ToString();
                if (ds.Tables[0].Rows[0]["CreateDate"].ToString() != "")
                {
                    model.CreateDate = ds.Tables[0].Rows[0]["CreateDate"].ToString();
                }
                if (ds.Tables[0].Rows[0]["Author"].ToString() != "")
                {
                    model.Author = ds.Tables[0].Rows[0]["Author"].ToString();
                }
                return model;
            }
            return null;
        }
    }
}